#include "callRPG.h"

void initMap() {
    int i, j, k;

    struct UserData * data = (UserData*) esdlGetUserData();
    data->dx = 0;
    data->dy = 0;
    data->dz = 0;

    for (k = 0; k < NUMBER_MAP; k++)
        for (i = 0; i < WIDTH; i++) {
            for (j = 0; j < LENGTH; j++) {
                data->map[i][j][k].affichage = 0;
                data->map[i][j][k].contenu = 0;

            }
        }
}

void printMap() {
    struct UserData * data = (UserData*) esdlGetUserData();
    int i;
    int j = 0;

    for (i = 0; i < WIDTH; i++) {
        for (j = 0; j < LENGTH; j++) {
            printf("%d ", data->map[i][j][data->dz].affichage);
        }
        printf("\n");
    }



}

void printWindows() {
    struct UserData * data = (UserData*) esdlGetUserData();
    int i, j;
    for (i = 0; i < WIDTH; i++) {
        for (j = 0; j < LENGTH; j++) {
            if (data->map[i][j][data->dz].affichage == 1) {
                data->wallSprite[i * INCREASE][j * INCREASE] = esdlLoadSpriteFromFile("wall2.png");
                esdlSetSpritePosition(data->wallSprite[i * INCREASE ][j * INCREASE], j * INCREASE, i * INCREASE);
            }
        }
    }
}

void createBoard() {
    struct UserData * data = (UserData*) esdlGetUserData();
    int i, j = 0;

    /*  for (i = 0; i < WIDTH; i++) {
          for (j = 0; j < LENGTH; j++) {
              if (i == WIDTH / 2) {
                  if (j >= LENGTH / 3 && j < LENGTH - LENGTH / 3) {
                     data->map[i][j][data->dz].affichage = 1;


                  }
              }
          }
      }

      for (i = 0; i < WIDTH; i++) {
          for (j = 0; j < LENGTH; j++) {
              if (j == LENGTH / 2) {
                  if (i >= WIDTH / 3 && i < WIDTH - WIDTH / 3) {
                      data->map[i][j][data->dz].affichage = 1;


                  }
              }
          }
      }

      for (i = 0; i < WIDTH; i++) {
          for (j = 0; j < LENGTH; j++) {
              if ((i == 0) || (i == WIDTH - 1)) {
                  data->map[i][j][data->dz].affichage = 1;

              }
              if (((j == 0) || (j == LENGTH - 1)) && (i != 0)) {
                  data->map[i][j][data->dz].affichage = 1;
              }
          }
      }
     */
    for (i = 0; i < WIDTH; i++) {
        for (j = 0; j < LENGTH; j++) {

            if (i == 0) {
                if ((j >= 0) && (j < 30)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j > 35) && (j < 70)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }

            if (i == 39) {
                if ((j >= 0) && (j < 30)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j > 35) && (j < 70)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }

            if (i == 16) {
                if ((j >= 0) && (j < 10)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j >= 15) && (j < 30)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j > 35) && (j < 52)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j >= 57) && (j < 70)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }

            if (i == 23) {
                if ((j >= 0) && (j < 10)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j >= 15) && (j < 30)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j > 35) && (j < 52)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((j >= 57) && (j < 70)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }


            if (j == 0) {
                if ((i >= 0) && (i < 17)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((i > 22) && (i < 40)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }

            if (j == 69) {
                if ((i >= 0) && (i < 17)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((i > 22) && (i < 40)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }

            if (j == 29) {
                if ((i >= 0) && (i < 17)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((i > 22) && (i < 40)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }

            if (j == 35) {
                if ((i >= 0) && (i < 17)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
                if ((i > 22) && (i < 40)) {

                    data->map[i][j][data->dz].affichage = 1;

                }
            }
        }
    }

}

void fondPremier() {

    struct UserData * data = (UserData*) esdlGetUserData();
    struct peopleFond * deco = (peopleFond*) esdlGetUserData();

    esdlDeleteSprite(data->marker);
    esdlDeleteSprite(deco->c3po);
    esdlDeleteSprite(deco->vador);
    esdlDeleteSprite(deco->yoda);
    esdlDeleteSprite(deco->ecran);

    data->marker = esdlLoadSpriteFromFile("personnages/r2d3.gif");
    if (data->marker == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }
    esdlSetSpritePosition(data->marker, 640, 740);

    deco->c3po = esdlLoadSpriteFromFile("personnages/C3PO.gif");
    if (deco->c3po == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }
    esdlSetSpritePosition(deco->c3po, 240, 590);

    deco->c3po = esdlLoadSpriteFromFile("personnages/yoda.gif");
    if (deco->c3po == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }
    esdlSetSpritePosition(deco->c3po, 240, 100);
     
     esdlDeleteSprite(deco->ecran);
     deco->ecran= esdlLoadSpriteFromFile("fond/map_11.jpg");
    if (deco->ecran == NULL) {     
        printf("Unable to load resources...\n");
        exit(1);
    } 


}

void fondSecond() {
    struct UserData * data = (UserData*) esdlGetUserData();
    struct peopleFond * deco = (peopleFond*) esdlGetUserData();

    esdlDeleteSprite(data->marker);
    esdlDeleteSprite(deco->c3po);
    esdlDeleteSprite(deco->vador);
    esdlDeleteSprite(deco->yoda);
    esdlDeleteSprite(deco->ecran);

    data->marker = esdlLoadSpriteFromFile("personnages/r2d3.gif");
    if (data->marker == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }
    esdlSetSpritePosition(data->marker, 640, 740);

    deco->c3po = esdlLoadSpriteFromFile("personnages/C3PO.gif");
    if (deco->c3po == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }
    esdlSetSpritePosition(deco->c3po, 240, 590);

    deco->c3po = esdlLoadSpriteFromFile("personnages/yoda.gif");
    if (deco->c3po == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }
    esdlSetSpritePosition(deco->c3po, 240, 100);

    esdlDeleteSprite(deco->ecran);
    deco->ecran = esdlLoadSpriteFromFile("fond/map_12.jpg");
    if (deco->ecran == NULL) {
        printf("Unable to load resources...\n");
        exit(1);
    }

}